package com.his.his_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.his.his_system.domain.DzmHisMember;
import com.his.his_system.domain.LoginUser;
import com.his.his_system.mapper.DzmHisMemberMapper;
import com.his.his_system.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Objects;
@Service
public class LoginServiceImpl implements LoginService , UserDetailsService {

    @Autowired
    DzmHisMemberMapper dzmHisMemberMapper;

    @Autowired
    HttpServletRequest request;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        String ip = request.getRemoteAddr();
        System.out.println(ip);

        System.out.println("查询数据库验证用户名密码");
        DzmHisMember user = dzmHisMemberMapper.selectByUserName(username);

        if (Objects.isNull(user)) {
            throw new RuntimeException("用户名或密码错误");
        }

        return new LoginUser(user);
    }
}
